<template>
	<view class="content">
		<view class="mainTitle">服务地址</view>
		<view class="box flex">
			<view class="address">
				<view class="tip">{{addressInfo.township}}</view>
				<view class="detail">{{addressInfo.address}}</view>
			</view>
			<uni-icons type='right' size='15'></uni-icons>
		</view>
		<view class="box" style="border-radius: 30rpx;">
			<view class="form-item">
				<view class="name">详细地址</view>
				<view class="value">
					<input type="text" class="input" v-model="seriveParams.fullAddress" placeholder="请填写详细地址" />
				</view>
			</view>
			<view class="form-item">
				<view class="name">联系人</view>
				<view class="value">
					<input type="text" class="input" v-model="seriveParams.linkman" placeholder="请填写姓名" />
					<uni-data-checkbox class="sex" v-model="radio1" :localdata="sex" selectedColor='#02B1AA'></uni-data-checkbox>
				</view>
			</view>
			<view class="form-item">
				<view class="name">联系电话</view>
				<view class="value">
					<input type="tel" class="input" v-model="seriveParams.linkPhone" placeholder="请输入联系电话" />
				</view>
			</view>
		</view>
		<view class="mainTitle">
			车辆信息
		</view>
		<view class="box" v-if='seriveParams.cartId'>
			<car-item :isCx='true' @again='goSelectCar' :cartInfo="selectCartInfo"></car-item>
		</view>
		<view class="box flexS" v-else @click="goSelectCar">
			<uni-icons type="plus-filled" size="20" color='#02B1AA'></uni-icons>
			<text>请选择车辆</text>
		</view>
		
		<view class="mainTitle">
			洗车方案
		</view>
		<view class="box" v-if='seriveParams.menuId'>
		 <plan-item :isCx='true' @again='choosePlans' :planInfo="selectPlanInfo"></plan-item>
		</view>
		<view class="box flexS" v-else @click="choosePlans">
			<uni-icons type="plus-filled" size="20" color='#02B1AA'></uni-icons>
			<text>请选择洗车方案</text>
		</view>
		
		<view class="mainTitle">
			期望服务时间
		</view>
		<view class="box select-time">
			<view class="days">
				<view class="day" :class="currDay===i?'active':''" 
				v-for="(item,i) in days" :key="i"
				@click="currDay = i; currTime = 0"
				>
					{{item.label}}
				</view>
			</view>
			<view class="times">
				<view class="time" :class="currTime===i?'active':''" 
				v-for="(item,i) in times" :key="i"
				@click="currTime = i"
				>
					{{item.label}}
				</view>
			</view>
		</view>
		<view class="box">
			<view class="form-item" style="border: 0;">
				<view class="name">备注</view>
				<view class="value">
					<input type="text" class="input" v-model="seriveParams.remark" placeholder="请填写备注" />
				</view>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<view class="footerBtn">
			<view class="allPrice">
				<text class="all">合计:&nbsp;</text>
				<text class="dw">￥</text>
				<text class="price">{{selectPlanInfo.cost ? selectPlanInfo.cost : 0}}</text>
			</view>
			<button class="payBtn" type="default" :loading='falg' @click="surePay">确认支付</button>
		</view>
	</view>
</template>

<script>
   import config from '@/untils/configs'
   import {predetermine, payFail} from '@/untils/api.js'
	export default {
		data() {
			return {
			  days:[],
			  times: config.TIME,
			  currDay:0,
			  currTime:0,
			  addressInfo:null,
			  sex: [{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 2
				}],
				falg:false,
				radio1: 1,
				selectCartInfo:null,
				selectPlanInfo:null,
				seriveParams:{
					  lng: 0,
					  lat: 0,
					  gpsAddress: "",
					  fullAddress: "",
					  linkman: "",
					  linkPhone: "",
					  cartId: '',
					  menuId: '',
					  appointmentTime: "",
					  remark: ""
				}
			}
		},
		watch:{
		  currDay:{
			  handler(newV){
				  if(newV === 0){
					  let times = JSON.parse(JSON.stringify(config.TIME))
					  let Hours = new Date().getHours();
					  let selectArtileType = (Hours) => {
						  let num = 0
						  if(8<=Hours<16){
							 switch(Hours){
							 	 case 8:num = 1
								   break;
								 case 9:num = 2
								   break;
								 case 10:num = 3
								   break;
								 case 11:num = 4
								   break;
								 case 12:num = 4
								   break;
								 case 13:num = 4
								   break;
								 case 14:num = 5
								   break;
								 case 15:num = 6
								   break;
								 case 16:num = 7
								   break;
								 case 17:num = 7
								   break;					  
							 } 
						  }
						  if(Hours<8){
							  num = 0
						  }
						  if(Hours>=16){
						  	  num = 7
						  }
						  return num
					  }
					  let spliceNum = selectArtileType(Hours);
					  times.splice(0, spliceNum)
					  this.times = times
				  }else{
					  this.times = config.TIME
				  }
			  },
			  immediate:true
		  }	
		},
		created(){
			console.log('-----created')
		},
		onShow(){
			const  seriveParams = this.$getStorageSync('seriveParams') || ''
			if(seriveParams){
				this.seriveParams = seriveParams
			}
			this.selectCartInfo = this.$getStorageSync('selectCartInfo') || ''
			// if(selectCartInfo.id){
			// 	this.selectCartInfo = selectCartInfo;
			// }
			this.selectPlanInfo = this.$getStorageSync('selectPlanInfo') || ''
			// if(selectPlanInfo.id){this.selectPlanInfo = selectPlanInfo;}
			
		},
		onLoad(){
			console.log('-----onLoad')
		},
		mounted() {
			let option = this.$mp.query;
			if(option.cartId){
				this.seriveParams = this.$getStorageSync('seriveParams')
				this.seriveParams.cartId = option.cartId
				this.seriveParams.menuId = option.menuId
				this.currTime = this.$getStorageSync('currTime')
				this.currDay = this.$getStorageSync('currDay')
				if(option.cartId){
					let selectCartInfo = this.$getStorageSync('selectCartInfo') || ''
					this.selectCartInfo = selectCartInfo
				}
				if(option.menuId){
					let selectPlanInfo = this.$getStorageSync('selectPlanInfo') || ''
					this.selectPlanInfo = selectPlanInfo
				}
				setTimeout(() => {
					this.$removeStorageSync('currTime')
					this.$removeStorageSync('currDay')
				},1000)
			}else{
			  const defalutCart = this.$getStorageSync('defaultCart') || ''
			  if(this.$mp.query.menuId){
				  let selectPlanInfo = this.$getStorageSync('selectPlanInfo') || ''
				  console.log('selectPlanInfo---', selectPlanInfo)
				  this.selectPlanInfo = selectPlanInfo
				  this.seriveParams.menuId = option.menuId
				  if(defalutCart && defalutCart.cartType === selectPlanInfo.cartType){
					  this.$setStorageSync('selectCartInfo', defalutCart)
					  this.selectCartInfo = defalutCart
					  this.seriveParams.cartId = defalutCart.id
				  }
			  }else{
				  this.$setStorageSync('selectCartInfo', defalutCart)
				  this.selectCartInfo = defalutCart
				  this.seriveParams.cartId = defalutCart?.id
			  }
			  
			}
			for(let i=0;i<2;i++){
				let date = new Date().getTime() + i*86400000;
				this.days.push(this.getDateFilter(date, i))
			}
			let addressInfo = this.$getStorageSync('address')
			this.addressInfo =  addressInfo
		},
		methods: {
		  getDateFilter(time, i){
			let date = new Date(time);
			let year = date.getFullYear()
			let month = (date.getMonth()+1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
			let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
			let week = date.getDay();
            if(i===0){
				return {label:`${month}月${day}日(今天)`, value:`${year}-${month}-${day}`}
			}else if(i === 1){
				return {label:`${month}月${day}日(明天)`, value:`${year}-${month}-${day}`}
			}else{
				return {label:`${month}月${day}日(${config.WEEKS[week]})`, value:`${year}-${month}-${day}`}
			}
		  },
		  choosePlans(){
			  // if(!this.seriveParams.cartId){
				 //  uni.showToast({
				 //     icon:'none',
				 //  	title:'请先选择车辆'
				 //  })
				 //  return false;
			  // }
			  // currDay:0,
			  // currTime:0,
			  this.$setStorageSync('seriveParams', this.seriveParams)
			  this.$setStorageSync('currTime', this.currTime)
			  this.$setStorageSync('currDay', this.currDay)
			  uni.navigateTo({
			  	url:`/subpackage/orderWashCar/selectPlanInfo?cartId=${this.seriveParams.cartId}`
			  })
		  },
          goSelectCar(){
			 this.$setStorageSync('seriveParams', this.seriveParams)
			 this.$setStorageSync('currTime', this.currTime)
			 this.$setStorageSync('currDay', this.currDay)
			 uni.navigateTo({
			 	url:'/subpackage/orderWashCar/selectCar',
				success:() => {
				  // this.seriveParams.menuId = ''
				  // this.$setStorageSync('seriveParams', this.seriveParams)
				}
			 })
		  },
		  payFail(sn){
			  payFail({sn:sn}).then((res) => {
				uni.showToast({
					icon:'none',
					title:'支付失败'
				})
			  })
		  },
		  surePay(){
			  if(this.times.length === 0){
				  uni.showToast({
					  icon:'none',
					  title:'今日已无服务时间，请选择其他服务时间！'
				  })
				  return false;
			  }
			  if(this.falg){
				  return false
			  }
			  this.falg = true
			  let location = this.$getStorageSync('location')
			  this.seriveParams.appointmentTime = this.days[this.currDay].value+' '+this.times[this.currTime].value;
			  this.seriveParams.lng = location.longitude
			  this.seriveParams.lat = location.latitude
			  this.seriveParams.sex = this.radio1
			  let msg = '';
			  if(!this.seriveParams.fullAddress){
				  msg = '请填写详细地址'
			  }
			  if(!this.seriveParams.linkman){
			  	  msg = '请填写联系人姓名'
			  }
			  if(!this.seriveParams.linkPhone){
			  	  msg = '请填写联系电话'
			  }
			  const phoneReg = /^1[3456789]\d{9}$/
			  if(!phoneReg.test(this.seriveParams.linkPhone)){
				  msg = '填写的联系电话有误'
			  }
			  if(!this.seriveParams.cartId){
			  	  msg = '请选择车辆'
			  }
			  if(!this.seriveParams.menuId){
			  	  msg = '请选择洗车方案'
			  }
			  if(msg){
				  setTimeout(()=>{
					  this.falg = false
					  uni.showToast({
					  	icon:'none',
					  	title:msg,
					  })
				  },200)
				  
				  return false;
			  }
		   this.seriveParams = {...this.seriveParams, gpsAddress:this.addressInfo.address}
			  predetermine(this.seriveParams).then((data) => {
				  if(data.code - 200 !== 0){
					  this.falg = false
					  uni.showToast({
					  	icon:'none',
					  	title: data.msg,
					  })
					 return false; 
				  }
				  
				  this.$setStorageSync('seriveParams', this.seriveParams)
				  const {timeStamp, nonceStr, signType, packageValue, paySign, sn} = data.data
				  const _this = this;
				  wx.requestPayment({
					timeStamp,  //时间戳
					nonceStr,   //随机字符串
					package: packageValue,    //prepay_id
					signType,   //签名算法MD5
					paySign,    //签名
					success (res) {
						_this.falg = false
						if (res.errMsg == "requestPayment:ok"){
							console.log('支付成功', res)
							uni.redirectTo({
								url:'/subpackage/orderWashCar/paySuccess',
								success:() => {
									// this.$removeStorageSync('seriveParams')
									_this.$removeStorageSync('selectPlanInfo')
									_this.$removeStorageSync('selectCartInfo')
								}
							})
						}else{
							console.log('支付失败')
							_this.payFail(sn)
						}
					},
					fail (res) { 
						console.log('支付失败', res)
						_this.falg = false
						_this.payFail(sn)
					}
				})
				  
			  })
		  }
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		height: 100%;
		overflow: auto;
		background: #f8f8f8;
		.box{
			box-sizing: border-box;
			padding: 20rpx;
			margin-bottom: 30rpx;
			font-size: 30rpx;
			.detail{
				color: #AEB3B3;
			}
		}
		.flex{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.flexS{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			color: #02B1AA;
			text{
				text-indent: 20rpx;
			}
		}
		.form-item{
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 100rpx;
			border-bottom: 1px solid #e8e8e8;
			.name{
				width: 160rpx;
				text-indent: 20rpx;
			}
			.value{
				width: calc(100% - 160rpx);
				display: flex;
				justify-content: flex-start;
				.input{
					width: 200rpx;
				}
			}
		}
		.select-time{
			overflow: hidden;
			
			.days{
				float: left;
				width: 240rpx;
				text-align: center;
				margin-right: 40rpx;
				.day{
					height: 70rpx;
					line-height: 70rpx;
					border: 1px solid #eee;
					margin-bottom: 20rpx;
					border-radius: 10rpx;
					background: #fff;
				}
				.day.active{
					background: #02B1AA;
					color: #fff;
				}
			}
			.times{
				float: left;
				text-align: center;
				.time{
					height: 70rpx;
					line-height: 70rpx;
					border: 1px solid #eee;
					width: 240rpx;
					margin-bottom: 20rpx;
					border-radius: 10rpx;
				}
				.time.active{
					background: rgba(2, 177, 170, 0.12);
				}
			}
		}
		.footerBtn{
			width: 100%;
			background-color: #fff;
			height: 180rpx;
			position: fixed;
			bottom: 0;
			z-index: 999;
			display: flex;
			justify-content: space-between;
			align-items: center;
			left: 0;
			.allPrice{
			  width: 220rpx;
			  text-align: right;
			  .all{
				  font-weight: 800;
				  font-size: 30rpx;
				  color: #2A2B2B;
			  }
			  .dw{
				  font-size: 24rpx;
				  color: #F96501;
			  }
			  .price{
				  font-size: 40rpx;
				  color: #F96501;
			  }
		  }
		  .payBtn{
			  color: #fff;
			  line-height: 88rpx;
			  height: 88rpx;
			  width: 240rpx;
			  height: 88rpx;
			  background: linear-gradient( 225deg, #8AF3B3 0%, #02B1AA 100%);
			  box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(2,177,170,0.3);
			  border-radius: 16rpx 16rpx 16rpx 16rpx;
			  font-size: 34rpx;
			  margin-right: 40rpx;
		  }
		}
	}
</style>
